import sklearn
from sklearn import tree
from sklearn.model_selection import GridSearchCV
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble  import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
import lightgbm as lgb
from xgboost import XGBClassifier as xg

class BaseClassfyModels():
    #梯度树
    def mx_GradientBoostingClassifier(self,train_x, train_y):
        nick_name="GradientBoostingClassifier(梯度树)"
        mx = GradientBoostingClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #提升算法 AdaBoost
    def mx_AdaBoostClassifier(self,train_x, train_y):
        nick_name = "AdaBoost提升算法"
        mx = AdaBoostClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #极限组合森林
    def mx_ExtraTreesClassifier(self,train_x, train_y):
        nick_name = "ExtraTrees(极限组合森林)"
        mx = ExtraTreesClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #随机数随机森林
    def mx_RandomForestClassifier(self,train_x, train_y):
        nick_name = "RandomForest(随机森林)"
        mx = RandomForestClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #元估计器
    def mx_BaggingClassifier(self,train_x, train_y):
        nick_name = "Bagging(套袋算法)"
        mx = BaggingClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #BernoulliNB
    def mx_BernoulliNB(self,train_x, train_y):
        nick_name = " BernoulliNB(伯努利贝叶斯分类)"
        mx = sklearn.naive_bayes.BernoulliNB()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #高斯贝叶斯
    def mx_GaussianNB(self,train_x, train_y):
        nick_name = "GaussianNB(高斯贝叶斯)"
        mx = sklearn.naive_bayes.GaussianNB()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    #随机梯度下降
    def mx_SGDClassifier(self,train_x, train_y):
        nick_name = "SGD(随机梯度下降)"
        mx = SGDClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)


    # 多项式朴素贝叶斯算法，Multinomial Naive Bayes，函数名，multinomialnb
    def mx_bayes(self,train_x, train_y):
        nick_name = "朴素贝叶斯算法"
        mx = MultinomialNB(alpha=0.01)
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # KNN近邻算法，函数名，KNeighborsClassifier
    def mx_knn(self,train_x, train_y):
        nick_name = "KNN近邻算法"
        mx = KNeighborsClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # 随机森林算法， Random Forest Classifier, 函数名，RandomForestClassifier
    def mx_forest(self,train_x, train_y):
        nick_name = "随机森林算法"
        mx = RandomForestClassifier(n_estimators=8)
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # 决策树算法，函数名，tree.DecisionTreeClassifier()
    def mx_dtree(self,train_x, train_y):
        nick_name = "决策树算法"
        mx = tree.DecisionTreeClassifier()
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # GBDT迭代决策树算法，Gradient Boosting Decision Tree，
    # 又叫 MART(Multiple Additive Regression Tree)，函数名，GradientBoostingClassifier
    def mx_GBDT(self,train_x, train_y):
        nick_name = "GBDT迭代决策树算法"
        mx = GradientBoostingClassifier(n_estimators=200)
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # SVM向量机算法，函数名，SVC
    def mx_svm(self,train_x, train_y):
        nick_name = "SVM支持向量机"
        mx = SVC(kernel='rbf', probability=True)
        mx.fit(train_x, train_y)
        return (mx,nick_name)

    # LightBGM轻梯度提升机
    def mx_lightBGM(self,train_x,train_y):
        nick_name = "LightGBM"
        mx = lgb.LGBMClassifier()
        mx.fit(train_x, train_y)
        return (mx, nick_name)

    # xgboost极端梯度提升
    def mx_xgboost(self,train_x,train_y):
        nick_name = "xgboost"
        mx = xg()
        mx.fit(train_x, train_y)
        return (mx, nick_name)

    # # SVM- cross向量机交叉算法，函数名，SVC
    # def mx_svm_cross(self,train_x, train_y):
    #     nick_name = "SVM向量机交叉算法"
    #     mx = SVC(kernel='rbf', probability=True)
    #     param_grid = {'C': [1e-3, 1e-2, 1e-1, 1, 10, 100, 1000], 'gamma': [0.001, 0.0001]}
    #     grid_search = GridSearchCV(mx, param_grid, n_jobs=1, verbose=1)
    #     grid_search.fit(train_x, train_y)
    #     best_parameters = grid_search.best_estimator_.get_params()
    #     mx = SVC(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True)
    #     mx.fit(train_x, train_y)
    #     return mx,nick_name

    def __init__(self,X_train,y_train,X_test,y_test):
        self.X_train=X_train
        self.y_train=y_train
        self.X_test=X_test
        self.y_test=y_test
